package com.assessment.entity;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.staff.entity.Staff;

@Entity
@Table(name="t_assessment_record")
public class AssessmentRecord 
{
	private Long id;
	private AssessmentProject assessmentProject;
	private Staff staff;
	private Date time;
	private Integer score;
		
	//业务数据
	private Long assessmentProjectId;        //考核项目Id
	private String assessmentProjectName;    //考核项目Name
	private Long staffId;                    //考核人Id
	private String staffName;                //考核人Name

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}
	@ManyToOne(cascade=CascadeType.ALL)
	public AssessmentProject getAssessmentProject() {
		return assessmentProject;
	}
	@ManyToOne(cascade=CascadeType.ALL)
	public Staff getStaff() {
		return staff;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getTime() {
		return time;
	}
	public Integer getScore() {
		return score;
	}
	@Transient
	public Long getAssessmentProjectId() {
		return assessmentProjectId;
	}
	@Transient
	public String getAssessmentProjectName() {
		return assessmentProjectName;
	}
	@Transient
	public Long getStaffId() {
		return staffId;
	}
	@Transient
	public String getStaffName() {
		return staffName;
	}

	public void setId(Long id) {
		this.id = id;
	}
	public void setAssessmentProject(AssessmentProject assessmentProject) {
		this.assessmentProject = assessmentProject;
	}
	public void setStaff(Staff staff) {
		this.staff = staff;
	}
	
	public void setTime(Date time) {
		this.time = time;
	}
	public void setScore(Integer score) {
		this.score = score;
	}
	public void setAssessmentProjectId(Long assessmentProjectId) {
		this.assessmentProjectId = assessmentProjectId;
	}
	public void setAssessmentProjectName(String assessmentProjectName) {
		this.assessmentProjectName = assessmentProjectName;
	}
	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public static Specification<AssessmentRecord> getSpecification( final AssessmentRecord assessmentRecord)
	{
		Specification<AssessmentRecord> spec = new Specification<AssessmentRecord>() {
			public Predicate toPredicate(Root<AssessmentRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != assessmentRecord && !StringUtils.isEmpty(assessmentRecord.getId())) {
					 Predicate  p1 =  cb.equal(root.get("id").as(String.class),assessmentRecord.getId());
					 list.add(p1);
				 }
				 if(null != assessmentRecord && !StringUtils.isEmpty(assessmentRecord.getStaff())) {
					 Predicate  p2 =  cb.equal(root.get("staff").as(Staff.class),assessmentRecord.getStaff());
					 list.add(p2);
				 }
				 if(null != assessmentRecord && !StringUtils.isEmpty(assessmentRecord.getStaff())) {
					 Predicate  p3 =  cb.equal(root.get("assessmentProject").as(AssessmentProject.class),assessmentRecord.getAssessmentProject());
					 list.add(p3);
				 }
				 if(null != assessmentRecord && !StringUtils.isEmpty(assessmentRecord.getStaff())) {
					 Predicate  p4 =  cb.like(root.get("time").as(String.class),"%"+assessmentRecord.getTime()+"%");
					 list.add(p4);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
